<template>
  <div class="app-container">
    <el-row :gutter="40" type="flex" justify="space-around" class="panel-group">
      <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
        <div class="card-panel" >
          <div class="card-panel-icon-wrapper icon-people">
            <svg-icon icon-class="chart" class-name="card-panel-icon" />
          </div>
          <div class="card-panel-description">
            <div class="card-panel-text">
              当日订单量
            </div>
            <count-to :start-val="0" :end-val="statisticsVo.orderSummary" :duration="2600" class="card-panel-num" />
          </div>
        </div>
      </el-col>
      <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
        <div class="card-panel" >
          <div class="card-panel-icon-wrapper icon-message">
            <svg-icon icon-class="chart" class-name="card-panel-icon" />
          </div>
          <div class="card-panel-description">
            <div class="card-panel-text">
              当月订单量
            </div>
            <count-to :start-val="0" :end-val="statisticsVo.monthOrderSummary" :duration="3000" class="card-panel-num" />
          </div>
        </div>
      </el-col>
      <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
        <div class="card-panel" >
          <div class="card-panel-icon-wrapper icon-money">
            <svg-icon icon-class="chart" class-name="card-panel-icon" />
          </div>
          <div class="card-panel-description">
            <div class="card-panel-text">
              总订单量
            </div>
            <count-to :start-val="0" :end-val="statisticsVo.allOrderSummary" :duration="3200" class="card-panel-num" />
          </div>
        </div>
      </el-col>

    </el-row>
    <el-row :gutter="40" type="flex" justify="space-around" class="panel-group">
      <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
        <div class="card-panel" @click="handleSetLineChartData('newVisitis')">
          <div class="card-panel-icon-wrapper icon-people">
            <svg-icon icon-class="money" class-name="card-panel-icon" />
          </div>
          <div class="card-panel-description">
            <div class="card-panel-text">
              当日营业额
            </div>
            <count-to :start-val="0"
                      :end-val="statisticsVo.orderMoneySummary?parseFloat(statisticsVo.orderMoneySummary):0"
                      :decimals="2" :duration="2600" class="card-panel-num"/>
          </div>
        </div>
      </el-col>
      <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
        <div class="card-panel" >
          <div class="card-panel-icon-wrapper icon-message">
            <svg-icon icon-class="money" class-name="card-panel-icon" />
          </div>
          <div class="card-panel-description">
            <div class="card-panel-text">
              当月营业额
            </div>
            <count-to :start-val="0"
                      :end-val="statisticsVo.monthOrderMoneySummary?parseFloat(statisticsVo.monthOrderMoneySummary):0"
                      :decimals="2" :duration="3000" class="card-panel-num"/>
          </div>
        </div>
      </el-col>
      <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
        <div class="card-panel" >
          <div class="card-panel-icon-wrapper icon-money">
            <svg-icon icon-class="money" class-name="card-panel-icon" />
          </div>
          <div class="card-panel-description">
            <div class="card-panel-text">
              总营业额
            </div>
            <count-to :start-val="0"
                      :end-val="statisticsVo.allOrderMoneySummary?parseFloat(statisticsVo.allOrderMoneySummary):0"
                      :decimals="2" :duration="3200" class="card-panel-num"/>
          </div>
        </div>
      </el-col>
    </el-row>
    <div class="bar-chart-tittle">
      订单统计信息
      <el-radio-group v-model="orderInfoDay" @input="changeInput">
        <el-radio-button label="30">一个月</el-radio-button>
        <el-radio-button label="60">两个月</el-radio-button>
      </el-radio-group>
    </div>
    <bar-chart :chart-data="lineChartData"></bar-chart>
    <div class="bar-chart-tittle">
      订单金额统计信息
      <el-radio-group v-model="orderAmountInfoDay" @input="changeInput2">
        <el-radio-button label="30">一个月</el-radio-button>
        <el-radio-button label="60">两个月</el-radio-button>
      </el-radio-group>
    </div>
    <bar-chart :chart-data="lineChartData2" :color="'#36a3f7'"></bar-chart>
  </div>
</template>

<script>
import { listStatistics, getStatistics, delStatistics, addStatistics, updateStatistics,getChargeOrderStatisticsVo,getStatisticsByMonth,getStatisticsMonthByMonth } from "@/api/business/statistics";
import BarChart from '@/views/dashboard/BarChart.vue'
import CountTo from 'vue-count-to'

export default {
  name: "Statistics",
  components: {
    CountTo,
    BarChart
  },
  data() {
    return {
      orderInfoDay:'30',
      orderAmountInfoDay:'30',
      orderInfo:null,
      orderAmountInfo:null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 充电订单统计表格数据
      statisticsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        beginTime: 0,
        chargeOrderId: 0,
        days: 0,
        deviceId: "",
        endTime: 0,
        port: 0
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      statisticsVo: {
        allOrderMoneySummary:0,
        allOrderSummary:0,
        monthOrderMoneySummary:0,
        monthOrderSummary:0,
        orderMoneySummary:0,
        orderSummary:0,
      },
      chartDatas:[],
      datetimes: [],
      chartDatas2:[],
      datetimes2: [],
      lineChartData: {},
      lineChartData2:{}
    };
  },
  created() {
    //this.getList();
    this.getChargeOrderStatisticsVo()

  },
  mounted() {
    this.$nextTick(() => {
      this.getStatisticsByMonth()
      this.getStatisticsMonthByMonth()
    })
  },
  methods: {
    changeInput(e){
      this.datetimes = []
      this.chartDatas = []
      this.queryParams.days = e
      this.getStatisticsByMonth()
    },
    changeInput2(e){
      this.datetimes2 = []
      this.chartDatas2 = []
      this.queryParams.days = e
      this.getStatisticsMonthByMonth()
    },
    async getStatisticsByMonth(){
      this.queryParams.days = this.orderInfoDay
      let res = await getStatisticsByMonth(this.queryParams)
      this.orderInfo = res.data
      res.data.chartDatas.forEach(item=>{

        this.datetimes.push(this.parseTime(item.dateTime, '{m}-{d}'))
        this.chartDatas.push(parseInt(item.val))
      })
      this.lineChartData = {
        chartDatas: this.chartDatas,
        datetimes: this.datetimes
      }
      console.log(this.lineChartData)
    },
    async getStatisticsMonthByMonth(){
      this.queryParams.days = this.orderAmountInfoDay
      let res = await getStatisticsMonthByMonth(this.queryParams)
      this.orderAmountInfo = res.data
      res.data.chartDatas.forEach(item=>{
        this.datetimes2.push(this.parseTime(item.dateTime, '{m}-{d}'))
        this.chartDatas2.push(item.val)
      })
      this.lineChartData2 = {
        chartDatas: this.chartDatas2,
        datetimes: this.datetimes2
      }
      console.log(this.lineChartData2)
    },
    async getChargeOrderStatisticsVo(){
      let res = await getChargeOrderStatisticsVo()
      this.statisticsVo = res.data
    },
    /** 查询充电订单统计列表 */
    getList() {
      this.loading = true;
      listStatistics(this.queryParams).then(response => {
        this.statisticsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderMoneySummary: null,
        agentId: null,
        chargeStationId: null,
        createTime: null,
        day: null,
        month: null,
        orderSummary: null,
        year: null,
        date: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加充电订单统计";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getStatistics(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改充电订单统计";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateStatistics(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStatistics(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除充电订单统计编号为"' + ids + '"的数据项？').then(function() {
        return delStatistics(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/statistics/export', {
        ...this.queryParams
      }, `statistics_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style lang="scss" scoped>
.panel-group {
  margin-top: 18px;

  .card-panel-col {
    margin-bottom: 32px;
  }

  .card-panel {
    height: 108px;
    cursor: pointer;
    font-size: 12px;
    position: relative;
    overflow: hidden;
    color: #666;
    background: #fff;
    box-shadow: 4px 4px 40px rgba(0, 0, 0, .05);
    border-color: rgba(0, 0, 0, .05);

    &:hover {
      .card-panel-icon-wrapper {
        color: #fff;
      }

      .icon-people {
        background: #40c9c6;
      }

      .icon-message {
        background: #36a3f7;
      }

      .icon-money {
        background: #f4516c;
      }

      .icon-shopping {
        background: #34bfa3
      }
    }

    .icon-people {
      color: #40c9c6;
    }

    .icon-message {
      color: #36a3f7;
    }

    .icon-money {
      color: #f4516c;
    }

    .icon-shopping {
      color: #34bfa3
    }

    .card-panel-icon-wrapper {
      float: left;
      margin: 14px 0 0 14px;
      padding: 16px;
      transition: all 0.38s ease-out;
      border-radius: 6px;
    }

    .card-panel-icon {
      float: left;
      font-size: 48px;
    }

    .card-panel-description {
      float: right;
      font-weight: bold;
      margin: 26px;
      margin-left: 0px;

      .card-panel-text {
        line-height: 18px;
        color: rgba(0, 0, 0, 0.45);
        font-size: 16px;
        margin-bottom: 12px;
      }

      .card-panel-num {
        font-size: 20px;
      }
    }
  }
}

@media (max-width:550px) {
  .card-panel-description {
    display: none;
  }

  .card-panel-icon-wrapper {
    float: none !important;
    width: 100%;
    height: 100%;
    margin: 0 !important;

    .svg-icon {
      display: block;
      margin: 14px auto !important;
      float: none !important;
    }
  }
}

.bar-chart-tittle {
  width: 100%;
  font-weight: bold;
  font-size: 20px;
  color: #409EFF;
  display: flex;
  justify-content: space-between;
  padding: 10px 30px;
}
</style>
